Laravel / Advanced / Image upload to google cloud
Image upload to google cloud
-
Google account settings
1.Create ne project
1. login to https://console.cloud.google.com/
2. Create new project
2. create json key
3. create bucket
-
Coding
1. CONFIGURATION FILES AND VARIABLES
Rename the json key generated to googlecloud.json and move/copy it into the config directory like so: config/googlecloud.json.
in the .env file
# GOOGLE CLOUD GOOGLE_CLOUD_PROJECT_ID=laravel-tutorial GOOGLE_CLOUD_STORAGE_BUCKET=laravel-pdf-bucket Create a file in in the config directory and name it googlecloud.php like so: config/googlecloud.php and pass in this code:
return [ /* |-------------------------------------------------------------------------- | Google Cloud configuration |-------------------------------------------------------------------------- | | This file is for storing the credentials for Google Cloud | | | | */ 'project_id' => env('GOOGLE_CLOUD_PROJECT_ID'), 'storage_bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET'), ]; 2.USING THE GOOGLE CLOUD PACKAGE
composer require google/cloud-storage Coding
in controller
use Google\Cloud\Storage\StorageClient; key
$googleConfigFile = file_get_contents(config_path('googlecloud.json')); $storage = new StorageClient([ 'keyFile' => json_decode($googleConfigFile, true) ]); bucket
$storageBucketName = config('googlecloud.storage_bucket'); $bucket = $storage->bucket($storageBucketName); read the file
$fileSource = fopen($publicPath, 'r'); upload file
$newFolderName = $request['firstname'].'_'.date("Y-m-d").'_'.date("H:i:s"); $googleCloudStoragePath = $newFolderName.'/'.$filename; /* Upload a file to the bucket. Using Predefined ACLs to manage object permissions, you may upload a file and give read access to anyone with the URL.*/ $bucket->upload($fileSource, [ 'predefinedAcl' => 'publicRead', 'name' => $googleCloudStoragePath ]); response
return response()->json([ "status" => "success", "message" => "PDF saved successfully ", "data" => [ "url" => url($fileStoragePath), "google_storage_url" => 'https://storage.cloud.google.com/'.$storageBucketName.'/'.$googleCloudStoragePath ] ]); If you see this error: ext-json is missing in composer.json, this solves it: "require": { "ext-json": "*" }, 4. Delete file
$storage = new StorageClient(); $bucket = $storage->bucket($bucketName); $object = $bucket->object($objectName); $object->delete(); -
Live Example
try { $user = Auth::user(); if ($request->hasFile('image')) { $snap=Snap::find($request->snap_id); $image = $request->file('image'); $fileName = $image->getClientOriginalName(); // Image::configure(['driver' => 'imagick']); $thumbImage = Image::make($image->path()) ->fit(500, 500) ->encode('jpg', 40); $keyFilePath = base_path(env('GOOGLE_CLOUD_KEY_FILE')); $keyFile = fopen($keyFilePath, 'r'); $keyFileContents = fread($keyFile, filesize($keyFilePath)); fclose($keyFile); $storage = new StorageClient([ 'projectId' => env('GOOGLE_CLOUD_PROJECT_ID'), 'keyFile' => json_decode($keyFileContents, true), ]); $bucket = $storage->bucket(env('GOOGLE_CLOUD_STORAGE_BUCKET')); if(isset($snap) && $snap->type==1){ //save compressed image to snap_images and snap_thumbnails $imagick = new Imagick(); $imagick->setResolution(200, 200); $imagick->readimage($image->path()); $imagick->setImageFormat('jpg'); $imagick->setImageCompression(Imagick::COMPRESSION_JPEG); $imagick->setImageCompressionQuality(60); $uploadedFile = $bucket->upload( $imagick->getImageBlob(), ['name' => 'snap_images/' . $fileName] ); } else{ //save original image to snap_select and compressed in snap_thumbnails $uploadedFile = $bucket->upload( fopen($image->getPathname(), 'r'), ['name' => 'snap_select/' . $fileName] ); } $uploadedFile = $bucket->upload( $thumbImage->stream(), ['name' => 'snap_thumbnails/' . $fileName] ); $uploadedFilePath = $uploadedFile->gcsUri(); $snap_create = new SnapImages(); $snap_create->snap_id = @$request->snap_id; $snap_create->cate_id = @$request->cate_id; $snap_create->path = $fileName; $snap_create->save(); if($snap_create) { return response()->json([ 'status' => 200, 'message' => 'Snap Image created successfully' ]); } else { return response()->json([ 'success' => false, 'message' => 'Not able to create Snap Image' ]); } } } catch (\Throwable $th) { return response()->json([ 'success' => false, 'message' => 'Something went wrong!!!', 'exception' => $th->getMessage(), 'code' => $th->getCode(), ]); } return "No image file found!";